﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFIDA.U9.SCM.SM.SOUIModel;
using UFSoft.UBF.UI.WebControlAdapter;
using UFSoft.UBF.UI.ControlModel;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.Engine.Builder;
using System.Data;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.Sys.Database;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.WebControls.Association.Adapter;
using UFSoft.UBF.UI.WebControls.Association;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using System.Collections;
using System.Collections.Specialized;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.UI.WebControls;
using UFIDA.U9.UI.PDHelper;
using UFSoft.UBF.Business;
using UFSoft.UBF.View.Query;
using UFSoft.UBF.UI.IView;
using UFIDA.U9.FI.ER.LoanBillUIModel;
using UFIDA.U9.YD.Cli.PlugUI;
using UFIDA.U9.YD.Cli.PlugUI.YD;
using ItemUIModel;
using ItemBListUIModel;
using UFIDA.U9.EAM.FA.FA_AssetCardBListUIModel;
using UFIDA.U9.EAM.FA.FA_AlterDocBListUIModel;

namespace UFIDA.U9.YD.Cli.PlugUI
{
    /// <summary>
    /// 卡片列表
    /// 控制 删除 按钮不能点击
    /// </summary>
    public class YDFA_AlterDocBListFormWebPart : UFSoft.UBF.UI.Custom.ExtendedPartBase
    {
        private FA_AlterDocBListFormWebPart part;
        IUFDataGrid DataGrid4;
        //配置文件XX.config文件拷贝到portal下，dll文件放置到portal/UILib下
        //配置文件以WebPartExtend开头
        public override void AfterInit(UFSoft.UBF.UI.IView.IPart Part, EventArgs args)
        {
            //首先调用原来的事件
            base.AfterInit(Part, args);
            part = Part as FA_AlterDocBListFormWebPart;

            if (part == null)
                return;

        }

        public override void BeforeEventProcess(IPart Part, string eventName, object sender, EventArgs args, out bool executeDefault)
        {
            #region 在按钮点击前事件,添加判断逻辑
            try
            {
                //默认执行后续
                executeDefault = true;

                if (PDContext.Current.UserCode == "interfaceuser") return;

                UFWebButton4ToolbarAdapter webButton = sender as UFWebButton4ToolbarAdapter;
                //webButton.ClientID就是界面控件的ID
                if (webButton != null && webButton.ClientID == "u_M_p0_BtnUnApprove")
                {
                    IList<IUIRecord> records = this.part?.Model.FA_AlterDoc.Cache.GetSelectRecord();
                    foreach (var rec in records)
                    {
                        //MainID为单头ID
                        object id = rec["MainID"];
                        string sql = $"select DescFlexField_PrivateDescSeg5 from FA_AlterDoc where id='{id}'";
                        object lyxt = "";
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out lyxt);

                        if (!string.IsNullOrEmpty(lyxt.ToString()))
                        {
                            throw new Exception($"来源系统[{lyxt}],不能手工执行该操作");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FA_AlterDocBListUIModelModel model = this.part.Model;
                UIPlugHelper.SetErrorMsg(model,model.FA_AlterDoc.FocusedRecord, null, ex.Message);
                executeDefault = false;//不再执行后续操作
                return;
            }
            #endregion
            base.BeforeEventProcess(Part, eventName, sender, args, out executeDefault);
        }




    }
}
